package com.vf.admin.common;

import com.vf.admin.utils.StringEx;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 視頻，音頻處理
 */
public class FFMpegUtil {

    //Windows下 ffmpeg.exe的路径  E:\ffmpeg-20181007-0a41a8b-win64-static\bin
 	String ffmpeg = Constants.ffmpegpath;

    //Linux与mac下  ffmpeg的路径
//    private static String ffmpegEXE = "/developer/ffmpeg-4.0/bin/ffmpeg";


 	/**
     * @param audioInputPath 音频1的路径
     * @param zhuanChangInputPath 音频2的路径
     * @param daxiao   视频与音频结合之后的音频的路径
     * @param audioOutPath           视频的长度 ,单位为 s
     * @throws Exception
     */
    // 调整音频大小
    // ffmpeg -i 2.mp3 -af volume=0.03 output.mp3
    public  void zhuanChangTeXiao(String audioInputPath,String zhuanChangInputPath,double daxiao,String audioOutPath)
            throws Exception {
    	String dir = audioOutPath.substring(0,audioOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
		
		audioOutPath = audioOutPath.replaceAll("\\\\","/");
		audioOutPath = audioOutPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
        command.add("-af");
        command.add("volume="+daxiao);
        command.add(audioOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }
    
    
    
 	/**
     * 裁剪视频画面的大小
     * ffmpeg  -i  11.mp4  -vf  crop=iw:390:0:350 -b:v 5000K 111.mp4
     */
    public void caiJianShiPinHuaMian(String videoInputPath,int StartY,int height,int kuan,int gao,String videoOutPath) throws Exception {
    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
		videoInputPath = videoInputPath.replaceAll("\\\\","/");
		videoInputPath = videoInputPath.replaceAll("//","/");
    	
		videoOutPath = videoOutPath.replaceAll("\\\\","/");
		videoOutPath = videoOutPath.replaceAll("//","/");
		
		System.out.println("videoInputPath:"+videoInputPath);
		System.out.println("videoOutPath:"+videoOutPath);
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(videoInputPath);
        command.add("-vf");
        command.add("crop=iw:"+height+":0:"+StartY);
        command.add("-b:v");
        command.add("8000K");
        if(kuan>0 || gao>0){
        	command.add("-s");
        	command.add(kuan+"x"+gao);
        }
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    
    /**
     * 两个视频合成为一个一屏双视频
     * ffmpeg -i 1.mp4 -i 1.mp4 -y -filter_complex [0:v]pad=w=iw*2:h=ih[bj1];[bj1][1:v]overlay=x=W/2 out1.mp4
     */
    public void hechengshuangshipin(String videoInputPath1,String videoInputPath2,int kuan,int gao,String videoOutPath) throws Exception {
    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
		videoInputPath1 = videoInputPath1.replaceAll("\\\\","/");
		videoInputPath1 = videoInputPath1.replaceAll("//","/");
		
		videoInputPath2 = videoInputPath2.replaceAll("\\\\","/");
		videoInputPath2 = videoInputPath2.replaceAll("//","/");
    	
		videoOutPath = videoOutPath.replaceAll("\\\\","/");
		videoOutPath = videoOutPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(videoInputPath1);
        command.add("-i");
        command.add(videoInputPath2);
        command.add("-y");
        command.add("-filter_complex");
        command.add("[0:v]pad=w=iw*2:h=ih[bj1];[bj1][1:v]overlay=x=W/2");
        command.add("-b:v");
        command.add("8000K");
        if(kuan>0 || gao>0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }
        
        command.add(videoOutPath);
        String mingling = "";
        for(String str :command){
        	mingling = mingling+str+" ";
        }
        System.out.println("mingling:"+mingling);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    /**
     * 三个视频合成为一个一屏三视频
     * ffmpeg -i 1.mp4 -i 1.mp4 -y -filter_complex [0:v]pad=w=iw*2:h=ih[bj1];[bj1][1:v]overlay=x=W/2 out1.mp4
     */
    public void hechengsanshipin(String videoInputPath1,String videoInputPath2,String videoInputPath3,int kuan,int gao,String videoOutPath) throws Exception {
    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
		videoInputPath1 = videoInputPath1.replaceAll("\\\\","/");
		videoInputPath1 = videoInputPath1.replaceAll("//","/");
		
		videoInputPath2 = videoInputPath2.replaceAll("\\\\","/");
		videoInputPath2 = videoInputPath2.replaceAll("//","/");
		
		videoInputPath3 = videoInputPath3.replaceAll("\\\\","/");
		videoInputPath3 = videoInputPath3.replaceAll("//","/");
    	
		videoOutPath = videoOutPath.replaceAll("\\\\","/");
		videoOutPath = videoOutPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(videoInputPath1);
        command.add("-i");
        command.add(videoInputPath2);
        command.add("-i");
        command.add(videoInputPath3);
        command.add("-y");
        command.add("-filter_complex");
        command.add("[0:v]pad=w=iw*3:h=ih[bj1];[bj1][1:v]overlay=x=W/3[bj2];[bj2][2:v]overlay=x=(W/3+W/3)");
        command.add("-b:v");
        command.add("8000K");
        if(kuan>0 || gao>0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }
        command.add(videoOutPath);
        String mingling = "";
        for(String str :command){
        	mingling = mingling+str+" ";
        }
        System.out.println("mingling:"+mingling);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    
 	/**
     * @param audioInputPath 音频1的路径
     * @param daxiao 音频2的路径
     * @param audioOutPath   视频与音频结合之后的音频的路径
     * @throws Exception
     */
    // 调整音频大小
    // ffmpeg -i 2.mp3 -af volume=0.03 output.mp3
    public  void updateYinPinDaXiao(String audioInputPath,double daxiao,String audioOutPath)
            throws Exception {
    	String dir = audioOutPath.substring(0,audioOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
		
		audioOutPath = audioOutPath.replaceAll("\\\\","/");
		audioOutPath = audioOutPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
        command.add("-af");
        command.add("volume="+daxiao);
        command.add(audioOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }
    
    
 	/**
     * @param audioInputPath1 音频1的路径
     * @param audioInputPath2 音频2的路径
     * @param shichang   视频与音频结合之后的音频的路径
     * @param audioOutPath           视频的长度 ,单位为 s
     * @throws Exception
     */
    // 两个音频文件重合合并
    // ffmpeg -i 1.mp3 -i output.mp3 -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f mp3 3.mp3
    public  void yinPinChongHeHeCheng(String audioInputPath1, String audioInputPath2, String shichang,String audioOutPath)
            throws Exception {
    	String dir = audioOutPath.substring(0,audioOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		audioInputPath1 = audioInputPath1.replaceAll("\\\\","/");
		audioInputPath1 = audioInputPath1.replaceAll("//","/");
		
		audioInputPath2 = audioInputPath2.replaceAll("\\\\","/");
		audioInputPath2 = audioInputPath2.replaceAll("//","/");
		
		audioOutPath = audioOutPath.replaceAll("\\\\","/");
		audioOutPath = audioOutPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath1);
        command.add("-i");
        command.add(audioInputPath2);
        command.add("-t");
        command.add(shichang);
        command.add("-filter_complex");
        command.add("amix=inputs=2:duration=first:dropout_transition=2");
        command.add("-f");
        command.add("mp3");
        command.add(audioOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }
    

 	/**
 	 * 把mp4转换为ts流文件
 	 * ffmpeg -i 1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts 1.ts
 	 */
 	public void mpsConverTs(String mpsFilePath,String tsPath) throws Exception{
 		String dir = tsPath.substring(0,tsPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
		mpsFilePath = mpsFilePath.replaceAll("\\\\","/");
		mpsFilePath = mpsFilePath.replaceAll("//","/");
    	
		tsPath = tsPath.replaceAll("\\\\","/");
		tsPath = tsPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(mpsFilePath);
        command.add("-c");
        command.add("copy");
        command.add("-bsf:v");
        command.add("h264_mp4toannexb");
        command.add("-f");
        command.add("mpegts");
        command.add(tsPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
 	}
 	
 	/**
 	 * 给mp4视频文件添加特效
 	 * ffmpeg -i 11.mp4  -itsoffset 00:00:02.300 -i zhuanchang.mp4 -y -itsoffset 00:00:05.300 -i zhuanchang.mp4 -y -filter_complex "[0:v]pad=w=iw:h=ih[beijing];[beijing][1:v]overlay=x=0:y=0[zhuanchang1];[zhuanchang1][2:v]overlay=x=0:y=0" timer_with_logo.mp4
 	 */
 	public void texiao(String inputShiPinPath,String[] inputTeXiaoPathArray,String outShipinPath,long[] waittimeArray,String[] tsjieshuStr,String shichang) throws Exception{
 		String dir = outShipinPath.substring(0,outShipinPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		if(inputShiPinPath==null){
			inputShiPinPath = "";
		}
		inputShiPinPath = inputShiPinPath.replaceAll("\\\\","/").replaceAll("//","/");
    	
		if(outShipinPath==null){
			outShipinPath = "";
		}
		outShipinPath = outShipinPath.replaceAll("\\\\","/").replaceAll("//","/");
		
		//获取转场特效时长
		if(inputTeXiaoPathArray!=null && inputTeXiaoPathArray.length>0){
			for(int e=0;e<inputTeXiaoPathArray.length;e++){
				String inputTeXiaoPath = inputTeXiaoPathArray[e];
				if(inputTeXiaoPath==null){
					inputTeXiaoPath = "";
				}
				inputTeXiaoPathArray[e] = inputTeXiaoPath.replaceAll("\\\\","/").replaceAll("//","/");
			}
		}
		
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(inputShiPinPath);
        
      //处理等待时长
      if(inputTeXiaoPathArray!=null && inputTeXiaoPathArray.length>0 && waittimeArray!=null && waittimeArray.length>0){
        	for(int e=0;e<waittimeArray.length;e++){
        		long waittime = waittimeArray[e];
        		String waittimestr = tsjieshuStr[e];
        		String texiao = inputTeXiaoPathArray[e];
        		
        		long zhuanchangshichanglong = this.getVideoTimeHaoMiao(texiao);
        		command.add("-itsoffset");
        		System.out.println("视频片段时间："+waittimestr+"_waittime:"+waittime+"_总时间："+waittimestr);
        		System.out.println("特效延迟时间："+new StringEx().getsfmhYuanDian(waittime-(zhuanchangshichanglong/2)));
            	command.add(new StringEx().getsfmhYuanDian(waittime-(zhuanchangshichanglong/2)));
            	command.add("-i");
            	command.add(texiao);
            	command.add("-y");
        	}
        }
        if(shichang!=null && !shichang.trim().equals("")){
        	command.add("-t");
            command.add(""+shichang);
        }
        
        //处理过滤器
        if(inputTeXiaoPathArray!=null && inputTeXiaoPathArray.length>0 && waittimeArray!=null && waittimeArray.length>0){
        	command.add("-filter_complex");
        	String filterName = "beijing";
        	String filterOldName = "beijing";
        	String mingling = "[0:v]pad=w=iw:h=ih["+filterName+"];";
        	long endtimeAll = 0l;
        	for(int e=0;e<waittimeArray.length;e++){
        		long waittime = waittimeArray[e];
        		String texiao = inputTeXiaoPathArray[e];
        		filterName = "beijing"+e;
        		if(e+1==waittimeArray.length){
        			mingling=mingling+"["+filterOldName+"]["+(e+1)+":v]overlay=x=0:y=0";
        		}else if(e+1<waittimeArray.length){
        			mingling=mingling+"["+filterOldName+"]["+(e+1)+":v]overlay=x=0:y=0["+filterName+"];";
        		}
        		filterOldName = filterName;
        	}
        	command.add(mingling);
        	command.add("-y");
        }
      //调整采样率和分辨率和比特率-------start-----
        command.add("-b:v");
        command.add("8000K");
        command.add("-s");
        command.add("1920x1080");
        command.add("-ar");
        command.add("48000");
//        command.add("-r");
//        command.add("29");
        command.add(outShipinPath);
        StringBuffer comf = new StringBuffer();
        for(String comd : command){
        	comf.append(comd+" ");
        }
        System.out.println("转场特效合成命令："+comf);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
 	}
 	
 	/**
 	 * 合成视频，后面的视频在指定的延迟时间进行重叠合并，使用场景：悬浮特效
 	 * ffmpeg -i 11.mp4  -itsoffset 00:00:02.300 -i zhuanchang.mp4 -y -itsoffset 00:00:05.300 -i zhuanchang.mp4 -y -filter_complex "[0:v]pad=w=iw:h=ih[beijing];[beijing][1:v]overlay=x=0:y=0[zhuanchang1];[zhuanchang1][2:v]overlay=x=0:y=0" timer_with_logo.mp4
 	 */
 	public void texiao(String inputShiPinPath,String inputTeXiaoPath,String outShipinPath,long waittime,String shichang) throws Exception{
 		String dir = outShipinPath.substring(0,outShipinPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		if(inputShiPinPath==null){
			inputShiPinPath = "";
		}
		inputShiPinPath = inputShiPinPath.replaceAll("\\\\","/").replaceAll("//","/");
    	
		if(inputTeXiaoPath==null){
			inputTeXiaoPath = "";
		}
		inputTeXiaoPath = inputTeXiaoPath.replaceAll("\\\\","/").replaceAll("//","/");
		
		
		if(outShipinPath==null){
			outShipinPath = "";
		}
		outShipinPath = outShipinPath.replaceAll("\\\\","/").replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(inputShiPinPath);
        
      //处理等待时长
        long zhuanchangshichanglong = this.getVideoTimeHaoMiao(inputTeXiaoPath);
		command.add("-itsoffset");
		System.out.println("视频片段时间："+waittime+"_等待时间字符："+new StringEx().getsfmhYuanDian(waittime));
		System.out.println("特效延迟时间："+new StringEx().getsfmhYuanDian(waittime));
    	command.add(""+new StringEx().getsfmhYuanDian(waittime));
    	command.add("-i");
    	command.add(inputTeXiaoPath);
    	command.add("-y");
    	
        if(shichang!=null && !shichang.trim().equals("")){
        	command.add("-t");
            command.add(""+shichang);
        }
        
        //处理过滤器
        command.add("-filter_complex");
    	command.add("[0:v]pad=w=iw:h=ih[beijing];[beijing][1:v]overlay=x=0:y=0");
    	command.add("-y");
    	
      //调整采样率和分辨率和比特率-------start-----
        command.add("-b:v");
        command.add("8000K");
        command.add("-s");
        command.add("1920x1080");
        command.add("-ar");
        command.add("48000");
        command.add(outShipinPath);
        StringBuffer comf = new StringBuffer();
        for(String comd : command){
        	comf.append(comd+" ");
        }
        System.out.println("转场特效合成命令："+comf);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
 	}
 	
 	
 	/**
 	 * 把多个ts流文件转换为拼接mp4
 	 * ffmpeg -i "concat:1.ts|2.ts|3.ts" -c copy -t 10 -bsf:a aac_adtstoasc -movflags +faststart output.mp4
 	 */
 	public void tsPinJieMps(String[] tsfileArray,String shichang,int kuan,int gao,String shipinPath) throws Exception{
 		String dir = shipinPath.substring(0,shipinPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		String tsfileStr = "";
		if(tsfileArray!=null && tsfileArray.length>0){
			for(String tsFilepath : tsfileArray){
				if(StringEx.isNotNull(tsFilepath)){
					tsfileStr = tsfileStr+tsFilepath+"|";
				}
			}
			tsfileStr = tsfileStr.substring(0, tsfileStr.length()-1);
		}
		tsfileStr = tsfileStr.replaceAll("\\\\","/");
		tsfileStr = tsfileStr.replaceAll("//","/");
    	
		shipinPath = shipinPath.replaceAll("\\\\","/");
		shipinPath = shipinPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add("\"concat:"+tsfileStr+"\"");
        
        // copy 不能与过滤器同时使用
        command.add("-c");
        command.add("copy");
        command.add("-bsf:a");
        command.add("aac_adtstoasc");
        command.add("-movflags");
        command.add("+faststart");
        if(shichang!=null && !shichang.trim().equals("")){
        	command.add("-t");
            command.add(""+shichang);
        }
      //调整采样率和分辨率和比特率-------start-----
//        command.add("-b:v");
//        command.add("5000K");
//        command.add("-s");
//        command.add("1920x1080");
//        command.add("-ar");
//        command.add("48000");
        
        command.add("-b:v");
        command.add("8000K");
        if(kuan>0 || gao>0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }else{
        	command.add("-s");
            command.add("1920x1080");
        }
        
        command.add("-ar");
        command.add("48000");
        command.add("-ac"); 
        command.add("2");
//        command.add("-r");
//        command.add("29");
        
        
        command.add(shipinPath);
        StringBuffer comf = new StringBuffer();
        for(String comd : command){
        	comf.append(comd+" ");
        }
        System.out.println("多个ts流文件合成命令："+comf);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
 	}
 	
 	/**
 	 * 把2个ts流文件转换为拼接mp4
 	 * ffmpeg -i "concat:1.ts|" -c copy -t 10 -bsf:a aac_adtstoasc -movflags +faststart output.mp4
 	 */
 	public void twoTsPinJieMps(String tsfileyi,String tsfileer,String shichang,String shipinPath) throws Exception{
 		String dir = shipinPath.substring(0,shipinPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		tsfileyi = tsfileyi.replaceAll("\\\\","/").replaceAll("//","/");
		tsfileer = tsfileer.replaceAll("\\\\","/").replaceAll("//","/");
		shipinPath = shipinPath.replaceAll("\\\\","/").replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        if(!StringEx.isNull(tsfileyi) && !StringEx.isNull(tsfileer)){
        	command.add("\"concat:"+tsfileyi+"|"+tsfileer+"\"");
        }else if(!StringEx.isNull(tsfileyi) && StringEx.isNull(tsfileer)){
        	command.add("\"concat:"+tsfileyi+"\"");
        }else if(StringEx.isNull(tsfileyi) && !StringEx.isNull(tsfileer)){
        	command.add("\"concat:"+tsfileer+"\"");
        }
        System.out.println();
        
        // copy 不能与过滤器同时使用
        command.add("-c");
        command.add("copy");
        command.add("-bsf:a");
        command.add("aac_adtstoasc");
        command.add("-movflags");
        command.add("+faststart");
        if(shichang!=null && !shichang.trim().equals("")){
        	command.add("-t");
            command.add(""+shichang);
        }
      //调整采样率和分辨率和比特率-------start-----
//        command.add("-b:v");
//        command.add("5000K");
//        command.add("-s");
//        command.add("1920x1080");
//        command.add("-ar");
//        command.add("48000");
        
        command.add("-b:v");
        command.add("8000K");
        command.add("-s");
        command.add("1920x1080");
        
        command.add("-ar");
        command.add("48000");
        command.add("-ac"); 
        command.add("2");
//        command.add("-r");
//        command.add("29");
        command.add(shipinPath);
        StringBuffer comf = new StringBuffer();
        for(String comd : command){
        	comf.append(comd+" ");
        }
        System.out.println("多个ts流文件合成命令："+comf);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
 	}
 	
 	/**
     * 提取视频里面的视频资源，把声音去掉
     * ffmpeg -i 0.mp4 -vcodec copy -an 1.mp4
     * @param audioInputPath
     *
     *
     *
     */
    public void tiQuShiPin(String audioInputPath,int kuan,int gao, String audiooutputPath)
            throws Exception {
    	String dir = audiooutputPath.substring(0,audiooutputPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
    	audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
    	
    	
		audiooutputPath = audiooutputPath.replaceAll("\\\\","/");
		audiooutputPath = audiooutputPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
        command.add("-b:v");
        command.add("8000K");
        if(kuan>0 || gao>0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }
        command.add("-ar");
        command.add("48000");
        
        command.add("-vcodec");
        command.add("copy");
        command.add("-an");
        command.add(audiooutputPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    
    /**
     * 视频与字幕、静止logo、悬浮指定时间显示的logo 三个同时进行合并
     * ffmpeg -i 1.mp4 -vf subtitles=1.srt out.mp4
     * 
     * ffmpeg -i 1.mp4 -i logo.jpg -itsoffset 1 -i 2.png -y -filter_complex "[0:v][1:v]overlay=1800:800[logo];[logo]subtitles=:filename=1.srt[sub];[2:v]colorkey=0x000000:0.1:0.2[ckout];[sub][ckout]overlay=x='if(gte(t,1), if(lte(t,4), 480, NAN),NAN)':y=360" -b:v 8000K -y output.mp4
     */
    public void shiPinZiMuLogoXuanFuLogoHeBing(String audioInputPath, String ziMuPath,String logoPath,int logoX,int logoY,String xuanfulogoPath,int xuanfulogoX,int xuanfulogoY,int xuanfustarttime,int xuanfuendtime,double yinliang,int kuan,int gao,String yinyuepath,String audioOutPut)
            throws Exception {
    	String dir = audioOutPut.substring(0,audioOutPut.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		dir = audioOutPut.substring(0,ziMuPath.lastIndexOf("/")+1);
		fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		dir = audioOutPut.substring(0,xuanfulogoPath.lastIndexOf("/")+1);
		fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
    	audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
    	
		if(ziMuPath!=null){
			ziMuPath = ziMuPath.replaceAll("\\\\","/");
			ziMuPath = ziMuPath.replaceAll("//","/");
		}
		
		if(logoPath!=null){
			logoPath = logoPath.replaceAll("\\\\","/");
			logoPath = logoPath.replaceAll("//","/");
		}
		
		if(xuanfulogoPath!=null){
			xuanfulogoPath = xuanfulogoPath.replaceAll("\\\\","/");
			xuanfulogoPath = xuanfulogoPath.replaceAll("//","/");
		}
		
		if(yinyuepath!=null){
			yinyuepath = yinyuepath.replaceAll("\\\\","/");
			yinyuepath = yinyuepath.replaceAll("//","/");
		}
		
		
		audioOutPut = audioOutPut.replaceAll("\\\\","/");
		audioOutPut = audioOutPut.replaceAll("//","/");
		
		//String xuanfulogoPath,int xuanfulogoX,int xuanfulogoY,int xuanfustarttime,int xuanfuendtime
//ffmpeg -i 1.mp4 -i logo.jpg -itsoffset 1 -i 2.png -y -filter_complex "[0:v][1:v]overlay=1800:800[logo];[logo]subtitles=:filename=1.srt[sub];[2:v]colorkey=0x000000:0.1:0.2[ckout];[sub][ckout]overlay=x='if(gte(t,1), if(lte(t,4), 480, NAN),NAN)':y=360" -b:v 8000K -y output.mp4		

		//ffmpeg -i 1.mp4 -i logo.jpg -itsoffset 1 -i 2.png -y -filter_complex "[0:v][1:v]overlay=1800:800[logo];[logo]overlay=x='if(gte(t,1), if(lte(t,4), 480, NAN),NAN)':y=360[bgg];[bgg]subtitles=:filename=1.srt" -b:v 8000K -y output.mp4		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
        if(logoPath!=null && !logoPath.trim().equals("")){
        	command.add("-i");
            command.add(logoPath);
        }
        if(xuanfulogoPath!=null && !xuanfulogoPath.trim().equals("")){
        	command.add("-itsoffset");
        	command.add("1");
        	command.add("-i");
            command.add(xuanfulogoPath);
        }
        
        if(yinyuepath!=null && !yinyuepath.trim().equals("")){
        	command.add("-i");
            command.add(yinyuepath);
        }
        
        command.add("-y");
        command.add("-filter_complex");
        
        String filterstr = "\"";
        if(logoPath!=null && !logoPath.trim().equals("")){
        	if(logoX>0 || logoY>0){
        		filterstr = filterstr+"[0:v][1:v]overlay="+logoX+":"+logoY+"";
        	}else{
        		filterstr = filterstr+"[0:v][1:v]overlay=1800:800";
        	}
        	if(xuanfulogoPath!=null && !xuanfulogoPath.trim().equals("")){
        		if(xuanfulogoX>0 || xuanfulogoY>0){
        			if(xuanfustarttime>0 || xuanfuendtime>0){
        				filterstr = filterstr+"[logo];[logo]overlay=x='if(gte(t,"+xuanfustarttime+"), if(lte(t,"+xuanfuendtime+"), "+xuanfulogoX+", NAN),NAN)':y="+xuanfulogoY;
        			}else{
        				filterstr = filterstr+"[logo];[logo]overlay=x='if(gte(t,0), if(lte(t,6), "+xuanfulogoX+", NAN),NAN)':y="+xuanfulogoY;	
        			}
        		}else{
        			if(xuanfustarttime>0 || xuanfuendtime>0){
        				filterstr = filterstr+"[logo];[logo]overlay=x='if(gte(t,"+xuanfustarttime+"), if(lte(t,"+xuanfuendtime+"), 0, NAN),NAN)':y=0";
        			}else{
        				filterstr = filterstr+"[logo];[logo]overlay=x='if(gte(t,0), if(lte(t,6), 0, NAN),NAN)':y=0";
        			}
        		}
            	if(ziMuPath!=null && !ziMuPath.trim().equals("")){
            		filterstr = filterstr+"[bgg];[bgg]subtitles=:filename="+ziMuPath;
            	}
            }else{
            	if(ziMuPath!=null && !ziMuPath.trim().equals("")){
            		filterstr = filterstr+"[bgg];[bgg]subtitles=:filename="+ziMuPath;
            	}
            }
        }else{
        	if(xuanfulogoPath!=null && !xuanfulogoPath.trim().equals("")){
        		if(xuanfulogoX>0 || xuanfulogoY>0){
        			filterstr = filterstr+"[0:v][1:v]overlay=x='if(gte(t,0), if(lte(t,6), "+xuanfulogoX+", NAN),NAN)':y="+xuanfulogoY;
        		}else{
        			filterstr = filterstr+"[0:v][1:v]overlay=x='if(gte(t,0), if(lte(t,6), 0, NAN),NAN)':y=0";
        		}
        		
        		if(ziMuPath!=null && !ziMuPath.trim().equals("")){
            		filterstr = filterstr+"[bgg];[bgg]subtitles=:filename="+ziMuPath;
            	}
        	}else{
        		if(ziMuPath!=null && !ziMuPath.trim().equals("")){
        			filterstr = filterstr+"[0:v]subtitles=:filename="+ziMuPath;
        		}
        	}
        }
        filterstr = filterstr+"\"";
        command.add(filterstr);
      //调整采样率和分辨率和比特率-------start-----
        command.add("-b:v");
        command.add("8000K");
        
        if(yinyuepath!=null && !yinyuepath.trim().equals("")){
        	command.add("-ar");
        	command.add("48000");
        	command.add("-ac");
        	command.add("2");
        }
        
        if(kuan>0 || gao>0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }
        if(yinliang>0){
        	command.add("-af");
        	command.add("volume="+yinliang+":eval=frame");
        }
        
        command.add("-y");
        command.add(audioOutPut);
        String mingling = "";
        for(String str :command){
        	mingling = mingling+str+" ";
        }
        System.out.println("mingling:"+mingling);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    
    
    /**
     * 视频与字幕合并
     * ffmpeg -i 1.mp4 -vf subtitles=1.srt out.mp4
     * 
     * ffmpeg -i H:/hunjian/nongcun/out.mp4 -vf movie=test2.png[wm];[in][wm]overlay=1800:800,scale=1920:1080,subtitles=:filename=H:/hunjian/nongcun/11.srt H:/hunjian/nongcun/out1.mp4
     */
    public void shiPinZiMuHeBing(String audioInputPath, String ziMuPath,String logoPath,int logoX,int logoY,double yinliang,int kuan,int gao,String audioOutPut)
            throws Exception {
    	String dir = audioOutPut.substring(0,audioOutPut.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
    	audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
    	
		if(ziMuPath!=null){
			ziMuPath = ziMuPath.replaceAll("\\\\","/");
			ziMuPath = ziMuPath.replaceAll("//","/");
		}
		
		if(logoPath!=null){
			logoPath = logoPath.replaceAll("\\\\","/");
			logoPath = logoPath.replaceAll("//","/");
		}
		
		audioOutPut = audioOutPut.replaceAll("\\\\","/");
		audioOutPut = audioOutPut.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
        command.add("-vf");
        int logo_XX = 0;
        int logo_YY = 0;
        if(logoX>0 || logoY>0){
        	logo_XX = logoX;
        	logo_YY = logoY;
        }
        if(logoPath!=null && !logoPath.trim().equals("") && ziMuPath!=null && !ziMuPath.trim().equals("")){
        	if(kuan>0 || gao>0){
        		command.add("movie=:filename="+logoPath+"[wm];[in][wm]overlay="+logo_XX+":"+logo_YY+",scale="+kuan+":"+gao+",subtitles=:filename="+ziMuPath);
        	}else{
        		command.add("movie=:filename="+logoPath+"[wm];[in][wm]overlay="+logo_XX+":"+logo_YY+",scale=1920:1080,subtitles=:filename="+ziMuPath);
        	}
        	
        }else if((logoPath==null || logoPath.trim().equals(""))  && ziMuPath!=null && !ziMuPath.trim().equals("")){
        	command.add("subtitles=:filename="+ziMuPath);
        }else if(logoPath!=null && !logoPath.trim().equals("") && (ziMuPath==null || ziMuPath.trim().equals(""))){
        	if(kuan>0 || gao>0){
        		command.add("movie=:filename="+logoPath+"[wm];[in][wm]overlay="+logo_XX+":"+logo_YY+",scale="+kuan+":"+gao+"");
        	}else{
        		command.add("movie=:filename="+logoPath+"[wm];[in][wm]overlay="+logo_XX+":"+logo_YY+",scale=1920:1080");
        	}
        	
        }
        if(yinliang>0){
        	command.add("-af");
        	command.add("volume="+yinliang+":eval=frame");
        }
      //调整采样率和分辨率和比特率-------start-----
        command.add("-b:v");
        command.add("8000K");
        if(kuan>0 || gao>0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }
        command.add("-ar");
        command.add("48000");
//        command.add("-r");
//        command.add("29");
        command.add(audioOutPut);
        String mingling = "";
        for(String str :command){
        	mingling = mingling+str;
        }
        System.out.println("mingling:"+mingling);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    /**
     * 视频与字幕合并
     * ffmpeg -i 1.mp4 -vf subtitles=1.srt out.mp4
     * 
     * ffmpeg -i H:/hunjian/nongcun/out.mp4 -vf movie=test2.png[wm];[in][wm]overlay=1800:800,scale=1920:1080,subtitles=:filename=H:/hunjian/nongcun/11.srt H:/hunjian/nongcun/out1.mp4 
     * @param
     */
    public void shiPinZiMuHeBing(String audioInputPath, String ziMuPath,int kuan,int gao,String audioOutPut)
            throws Exception {
    	String dir = audioOutPut.substring(0,audioOutPut.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
    	audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
    	
		ziMuPath = ziMuPath.replaceAll("\\\\","/");
		ziMuPath = ziMuPath.replaceAll("//","/");
		
		audioOutPut = audioOutPut.replaceAll("\\\\","/");
		audioOutPut = audioOutPut.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
        command.add("-vf");
        if(ziMuPath!=null && !ziMuPath.trim().equals("")){
        	command.add("subtitles=:filename="+ziMuPath);
        }
      //调整采样率和分辨率和比特率-------start-----
        command.add("-b:v");
        command.add("8000K");
        if(kuan>0 ||  gao>0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }
        command.add("-ar");
        command.add("48000");
        command.add(audioOutPut);
        String mingling = "";
        for(String str :command){
        	mingling = mingling+str;
        }
        System.out.println("mingling:"+mingling);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
 	/**
     * 分割视频片段
     * ffmpeg -ss 0 -i hecheng.mp4 -c copy -t  20  dst.mp4  ||   ffmpeg  -i 1.mp3 -c copy -ss 00:00:03.25 -to  00:00:05.200  00.mp3 
     * @param
     */
    public void fengeShiPin(String audioInputPath,String stattime,String endtime,String outPath)
            throws Exception {
    	System.out.println("分割视频片段>>>>>>>>>>>>>  stattime:"+stattime+"    endtime:"+endtime);
    	String dir = outPath.substring(0,outPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
    	
    	
		outPath = outPath.replaceAll("\\\\","/");
		outPath = outPath.replaceAll("//","/");
    	
		System.out.println("audioInputPath:"+audioInputPath);
		System.out.println("outPath:"+outPath);
		
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
//        command.add("-c");
//        command.add("copy");
        command.add("-ss");
        command.add(stattime);
        command.add("-to");
        command.add(endtime);
        
        command.add("-b:v");
        command.add("8000K");
        command.add("-s");
        command.add("1920x1080");
        
        command.add("-ar");
        command.add("48000");
        command.add("-ac"); 
        command.add("2");
//        command.add("-r");
//        command.add("29");
        command.add(outPath);
        StringBuffer comf = new StringBuffer();
        for(String comd : command){
        	comf.append(comd+" ");
        }
        System.out.println("视频按照指定的时间分割视频命令："+comf);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("执行中............");
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
        System.out.println("分割视频片段<<<<<<<<<<<<<<<<<<");

    }
    
    
 	/**
     * 分割视频里面的音频资源
     * ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3
     * @param
     */
    public void fenliYinPin(String audioInputPath, String videoOutPath)
            throws Exception {
    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
    	audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
		
		videoOutPath = videoOutPath.replaceAll("\\\\","/");
		videoOutPath = videoOutPath.replaceAll("//","/");
		
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
        command.add("-vn");
        command.add("-ar");
        command.add("44100");
        command.add("-ac");
        command.add("2");
        command.add("-ab");
        command.add("192");
        command.add("-f");
        command.add("mp3");
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }
    
    /**
     * 调节音量大小
     * ffmpeg -i 2.mp3 -af "volume='if(lt(t,1),0.2,if(lt(t,2),0.5,if(lt(t,2),0.7,if(gte(t,60),if(gte(t,61),if(gte(t,62),if(gte(t,63),0,0.2),0.4),0.6),1))))':eval=frame" 666.mp3
     * @param
     */
    public void tiaoJieYinLiang(String audioInputPath,int startmiao,int endmiao, String videoOutPath)
            throws Exception {
    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
    	audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
		
		videoOutPath = videoOutPath.replaceAll("\\\\","/");
		videoOutPath = videoOutPath.replaceAll("//","/");
		
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
        command.add("-af");
        command.add("volume='if(lt(t,"+startmiao+"),0.2,if(lt(t,"+(startmiao+1)+"),0.5,if(lt(t,"+(startmiao+2)+"),0.7,if(gte(t,"+endmiao+"),if(gte(t,"+(endmiao+1)+"),if(gte(t,"+(endmiao+2)+"),if(gte(t,"+(endmiao+3)+"),0.4,0.5),0.6),0.8),1))))':eval=frame");
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }
    
 	/**
     * @param videoInputPath 原视频的路径
     * @param videoOutPath   输出音频的路径
     * 注意只能提取aac后缀的隐僻文件
     */
    //  从视频中抽取mp3音频文件
    // ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aac
    public  void shipinOutYinPin(String videoInputPath, String videoOutPath)
            throws Exception {

    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(videoInputPath);
        command.add("-vn");
        command.add("-y");
        command.add("-acodec");
        command.add("copy");
        command.add(videoOutPath);
        String mingling = "";
        for(String minglingstr : command){
        	mingling = mingling+minglingstr+" ";
        }
        System.out.println(mingling);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }
    
    
    /**
     * @param videoInputPath 视频的输入路径
     * @param videoOutPath   视频的输出路径
     * @throws Exception
     */
    // 拷贝视频，并指定新的视频的名字以及格式
    // ffmpeg.exe -i old.mp4 new.avi
    public  void convetor(String videoInputPath, String videoOutPath) throws Exception {
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(videoInputPath);
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }
    
    /**
     * 同时调整视频采样率、声道和视频的分辨率、标特率     ffmpeg -i out.mp4 -b:v 8000K -s 1920x1080 -ar 48000 -ac 2  -y out1.mp4
     * 同时调整视频采样率、声道和视频的分辨率、标特率 和添加字幕 ffmpeg -i out.mp4 -b:v 8000K -s 1920x1080 -ar 48000 -ac 2  -vf subtitles=:filename=H:/hunjian/nongcun/11.srt -y out1.mp4
     *
     * 调整视频采样率、声道   ffmpeg -i 1.mp3 -ar 48000 -ac 2  -y  11.mp3
     * 
     * 调整分辨率 比特率 ffmpeg -i out.mp4 -b:v 8000K -s 1920x1080  out1.mp4
     * 
     * 如果字幕文件存在，则添加字幕
     */
    public void biTeLvConver(String videoInputPath,int kuan,int gao,String videoOutPath) throws Exception {
    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
		videoInputPath = videoInputPath.replaceAll("\\\\","/");
		videoInputPath = videoInputPath.replaceAll("//","/");
    	
		videoOutPath = videoOutPath.replaceAll("\\\\","/");
		videoOutPath = videoOutPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(videoInputPath);
        
        command.add("-b:v");
        command.add("8000K");
        if(kuan>0 || gao>0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }
        
        command.add("-ar");
        command.add("48000");
        command.add("-ac"); 
        command.add("2");
//        command.add("-r");
//        command.add("29");
        command.add("-y");
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    //ffmpeg -i 1.mp3 -i 11.mp4 -filter_complex amix=inputs=2:dropout_transition=0 -b:v 5000K -s 1920x1080  -ar  48000  -ac 2 a1.mp4(视频本身带有声音)
    /**
     * ffmpeg.exe -i 1.mp4 -i "1.mp3" -b:v 5000K -s 1920x1080 -ar  48000  -ac 2 -y new.mp4 (视频本身带有声音)
     * 使用滤镜混音(叠加) 录音文件前面  视频后面
     */
    public void lvjinghunyin(String videoInputPath, String audioInputPath, String videoOutPath)throws Exception {
    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		videoInputPath = videoInputPath==null ? "" : videoInputPath;
		videoInputPath = videoInputPath.replaceAll("\\\\","/");
		videoInputPath = videoInputPath.replaceAll("//","/");
		
		audioInputPath = audioInputPath==null ? "" : audioInputPath;
		audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
		
		videoOutPath = videoOutPath==null ? "" : videoOutPath;
		videoOutPath = videoOutPath.replaceAll("\\\\","/");
		videoOutPath = videoOutPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        if(audioInputPath!=null && !audioInputPath.trim().equals("")){
        	 command.add("-i");
             command.add(audioInputPath);
        }
        command.add("-i");
        command.add(videoInputPath);
        
        if(audioInputPath!=null && !audioInputPath.trim().equals("")){
            command.add("-filter_complex");
            command.add("amix=inputs=2:dropout_transition=0");
        }
        //调整采样率和分辨率和比特率-------start-----
        command.add("-b:v");
        command.add("8000K");
        command.add("-s");
        command.add("1920x1080");
        command.add("-ar");
        command.add("48000");
//        command.add("-r");
//        command.add("29");
        if(audioInputPath!=null && !audioInputPath.trim().equals("")){
        	command.add("-ac");
            command.add("2");
        }
        
      //调整采样率和分辨率和比特率-------end-----
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    //ffmpeg -i 1.mp3 -i 11.mp4 -filter_complex amix=inputs=2:dropout_transition=0 -b:v 5000K -s 1920x1080  -ar  48000  -ac 2 a1.mp4(视频本身带有声音)
    /**
     * ffmpeg.exe -i 1.mp4 -i "1.mp3" -b:v 5000K -s 1920x1080 -ar  48000  -ac 2 -y new.mp4  (视频本身没有声音)
     * 使用滤镜混音(叠加) 录音文件前面  视频后面
     */
    public void lvjinghunyinWuYin(String videoInputPath, String audioInputPath,int kuan,int gao, String videoOutPath)throws Exception {
    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		videoInputPath = videoInputPath==null ? "" : videoInputPath;
		videoInputPath = videoInputPath.replaceAll("\\\\","/");
		videoInputPath = videoInputPath.replaceAll("//","/");
		
		audioInputPath = audioInputPath==null ? "" : audioInputPath;
		audioInputPath = audioInputPath.replaceAll("\\\\","/");
		audioInputPath = audioInputPath.replaceAll("//","/");
		
		videoOutPath = videoOutPath==null ? "" : videoOutPath;
		videoOutPath = videoOutPath.replaceAll("\\\\","/");
		videoOutPath = videoOutPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        
        command.add("-i");
        command.add(videoInputPath);
        
        if(audioInputPath!=null && !audioInputPath.trim().equals("")){
        	 command.add("-i");
             command.add(audioInputPath);
        }
        
        //调整采样率和分辨率和比特率-------start-----
        command.add("-b:v");
        command.add("8000K");
        if(kuan>0 || gao>0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }
        command.add("-ar");
        command.add("48000");
//        command.add("-r");
//        command.add("29");
        if(audioInputPath!=null && !audioInputPath.trim().equals("")){
        	command.add("-ac");
            command.add("2");
        }
        
      //调整采样率和分辨率和比特率-------end-----
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    
    
    /**
     * @param videoInputPath 原视频的路径
     * @param audioInputPath 音频的路径
     * @param videoOutPath   视频与音频结合之后的视频的路径
     * @param time           视频的长度 ,单位为 s
     * @throws Exception
     */
    // 将视频和音频结合，并指定视频的长度，同时生成结合之后的视频文件
    // ffmpeg.exe -i tsd.mp4 -i "周笔畅+-+最美的期待.mp3" -t 7 -y new.avi
    
    // 将视频和音频结合，并指定视频的长度，同时调整分辨率和采样率生成结合之后的视频文件
    // ffmpeg.exe -i 1.mp4 -i "1.mp3" -t 10 -b:v 8000K -s 1920x1080 -ar 48000 -ac 2  -y hecheng11.mp4
    
    public  void shiPinYinPinHeCheng(String videoInputPath, String audioInputPath, String videoOutPath, String  time)
            throws Exception {

    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(videoInputPath);
        command.add("-i");
        command.add(audioInputPath);
        if(time!=null && !time.trim().equals("")){
        	command.add("-t");
            command.add(String.valueOf(time));
        }
        //调整采样率和分辨率和比特率-------start-----
        command.add("-b:v");
        command.add("8000K");
        command.add("-s");
        command.add("1920x1080");
        command.add("-ar");
        command.add("48000");
        command.add("-ac");
        command.add("2");
      //调整采样率和分辨率和比特率-------end-----
        
        command.add("-y");
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }


    /**
     * @param time_coverimg   视频的第几秒作为封面图
     * @param videoInputPath  视频的路径
     * @param frame           帧数
     * @param coverOutputPath 视频的封面图的路径
     * @throws Exception
     */
    // ffmpeg.exe -ss 00:00:01 -y -i 视频.mp4 -vframes 1 new.jpg   
    public  void convetor(String time_coverimg, String videoInputPath, int frame, String coverOutputPath)
            throws Exception {
    	String dir = coverOutputPath.substring(0,coverOutputPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
		videoInputPath = videoInputPath==null ? "" : videoInputPath;
		videoInputPath = videoInputPath.replaceAll("\\\\","/");
		videoInputPath = videoInputPath.replaceAll("//","/");
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-ss");
        command.add(time_coverimg);
        command.add("-y");
        command.add("-i");
        command.add(videoInputPath);
        command.add("-vframes");
        command.add(""+frame);
        command.add(coverOutputPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }

    
    /**
	 * 获取视频总时间
	 * @param video_path    视频路径
	 *  ffmpeg  -i aa.mp4 
	 * @return
	 */
	public  int getVideoTime(String video_path) {
		List<String> commands = new ArrayList<String>();
		Map<String, String> envmap = System.getenv();
		commands.add(ffmpeg);
		commands.add("-i");
		commands.add(video_path);
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commands);
			final Process p = builder.start();
			
			//从输入流中读取视频信息
	        BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
	        StringBuffer sb = new StringBuffer();
	        String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
                System.out.println(line);
            }
	        br.close();
	        
	        String regexVideo = "Video: (.*?), (.*?), (.*?)[,\\s]";
	        Pattern pattern = Pattern.compile(regexVideo);
	        Matcher m = pattern.matcher(sb.toString());
            if (m.find()) {
                System.out.println("编码格式：" + m.group(1) + ", 视频格式：" + m.group(2) + ", 分辨率：" + m.group(3) + "kb/s");
            }
            
	        //从视频信息中解析时长
	        String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
	        pattern = Pattern.compile(regexDuration);
	        m = pattern.matcher(sb.toString());
	        if (m.find()) {
	        	System.out.println("视频完整信息："+m.group(1));
	        	int time = getTimelen(m.group(1));
	        	System.out.println(video_path+",视频时长："+time+", 开始时间："+m.group(2)+",比特率："+m.group(3)+"kb/s");
	        	return time;
	        }
	        
	        

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return 0;
	}
	
	/**
	 * 获取视频总时间
	 * @param video_path    视频路径
	 *  ffmpeg  -i aa.mp4 
	 * @return
	 */
	public  String getVideoTimeStr(String video_path) {
		List<String> commands = new ArrayList<String>();
		Map<String, String> envmap = System.getenv();
		commands.add(ffmpeg);
		commands.add("-i");
		commands.add(video_path);
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commands);
			final Process p = builder.start();
			
			//从输入流中读取视频信息
	        BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
	        StringBuffer sb = new StringBuffer();
	        String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
                System.out.println(line);
            }
	        br.close();
	        
	        String regexVideo = "Video: (.*?), (.*?), (.*?)[,\\s]";
	        Pattern pattern = Pattern.compile(regexVideo);
	        Matcher m = pattern.matcher(sb.toString());
            if (m.find()) {
                System.out.println("编码格式：" + m.group(1) + ", 视频格式：" + m.group(2) + ", 分辨率：" + m.group(3) + "kb/s");
            }
            
	        //从视频信息中解析时长
	        String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
	        pattern = Pattern.compile(regexDuration);
	        m = pattern.matcher(sb.toString());
	        if (m.find()) {
	        	System.out.println("视频完整信息："+m.group(1));
	        	String time = m.group(1);
	        	System.out.println(video_path+",视频时长："+time+", 开始时间："+m.group(2)+",比特率："+m.group(3)+"kb/s");
	        	return time;
	        }
	        
	        

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "";
	}
	
	/**
	 * 获取视频总时间
	 * @param video_path    视频路径
	 *  ffmpeg  -i aa.mp4 
	 * @return
	 */
	public  long getVideoTimeHaoMiao(String video_path) {
		List<String> commands = new ArrayList<String>();
		Map<String, String> envmap = System.getenv();
		commands.add(ffmpeg);
		commands.add("-i");
		commands.add(video_path);
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commands);
			final Process p = builder.start();
			
			//从输入流中读取视频信息
	        BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
	        StringBuffer sb = new StringBuffer();
	        String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
                System.out.println(line);
            }
	        br.close();
	        
	        String regexVideo = "Video: (.*?), (.*?), (.*?)[,\\s]";
	        Pattern pattern = Pattern.compile(regexVideo);
	        Matcher m = pattern.matcher(sb.toString());
            if (m.find()) {
                System.out.println("编码格式：" + m.group(1) + ", 视频格式：" + m.group(2) + ", 分辨率：" + m.group(3) + "kb/s");
            }
            
	        //从视频信息中解析时长
	        String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
	        pattern = Pattern.compile(regexDuration);
	        m = pattern.matcher(sb.toString());
	        if (m.find()) {
	        	System.out.println("视频完整信息："+m.group(1));
	        	long time = StringConverLong(m.group(1));
	        	System.out.println(video_path+",视频时长："+time+", 开始时间："+m.group(2)+",比特率："+m.group(3)+"kb/s");
	        	return time;
	        }
	        
	        

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return 0;
	}
	
	/**
	 * 传入时间格式："0:0:0.0"
	 * @param shijian
	 * @return
	 */
	public long StringConverLong(String shijian){
		try{
			shijian = shijian.replaceAll(",",".");
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
		    Date d = sdf.parse(shijian);
		    SimpleDateFormat sdf2 =new SimpleDateFormat("HH:mm:ss.SSS");
		    Date d2 = sdf2.parse("0:0:0.0");
		    
		    long s =  d.getTime() - d2.getTime();
		    return s;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0l;
	}
	
	//格式:"00:00:10.68"
    private  int getTimelen(String timelen){
        int min=0;
        String strs[] = timelen.split(":");
        if (strs[0].compareTo("0") > 0) {
            min+=Integer.valueOf(strs[0])*60*60;//秒
        }
        if(strs[1].compareTo("0")>0){
            min+=Integer.valueOf(strs[1])*60;
        }
        if(strs[2].compareTo("0")>0){
            min+=Math.round(Float.valueOf(strs[2]));
        }
        return min;
    }

    
    
    /**
     * @param videoInputPath 原视频的路径
     * @param audioInputPath 音频的路径
     * @param videoOutPath   视频与音频结合之后的视频的路径
     * @param starttime     音频开始时间
     * @param shipinchangdu           视频的长度 ,单位为 s
     * @throws Exception
     */
    // 将视频和音频结合，并指定视频的长度，同时生成结合之后的视频文件
    // ffmpeg.exe -i tsd.mp4 -i "周笔畅+-+最美的期待.mp3" -t 7 -y new.avi
    public void convetorShiJian(String videoInputPath, String audioInputPath, String videoOutPath, double starttime,double shipinchangdu)
            throws Exception {
    	String dir = videoOutPath.substring(0,videoOutPath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-ss");
        command.add(String.valueOf(starttime));
        command.add("-t");
        command.add(String.valueOf(shipinchangdu));
        command.add("-i");
        command.add(audioInputPath);
        command.add("-i");
        command.add(videoInputPath);
        
        command.add("-y");
        command.add(videoOutPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }
    
    /**
     * 合并图片与视频
     * @param
     */
	//两个视频合并生成一个视频，同时视频显示声音
//	ffmpeg -i bj1.jpg -i touming.mov -y -filter_complex [0:v]pad=w=iw:h=ih[bj1];[bj1][1:v]overlay=0:0 -y out.mp4
    public void converPicShiPin(String picpath,String shipinpath,String savepath) throws Exception{
    	
    	String dir = savepath.substring(0,savepath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
		
    	List<String> command = new ArrayList<String>();
    	picpath = picpath.replaceAll("\\\\","/");
    	picpath = picpath.replaceAll("//","/");
    	
    	shipinpath = shipinpath.replaceAll("\\\\","/");
    	shipinpath = shipinpath.replaceAll("//","/");
    	
    	
    	savepath = savepath.replaceAll("\\\\","/");
    	savepath = savepath.replaceAll("//","/");
    	
    	command.add(ffmpeg);
        command.add("-i");
        command.add(picpath);
        command.add("-i");
        command.add(shipinpath);
        command.add("-y");
        command.add("-filter_complex");
        command.add("[0:v]pad=w=iw:h=ih[bj1];[bj1][1:v]overlay=0:0");
        command.add("-y");
        command.add(savepath);
        
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    /**
     * 合并图片与视频
     * @param
     */
	//两个视频合并生成一个视频，同时视频显示声音
//	ffmpeg -i bj1.jpg -i touming.mov -y -filter_complex [0:v]pad=w=iw:h=ih[bj1];[bj1][1:v]overlay=0:0 -y out.mp4
    public void converBgShiChangShiPin(String bgpath,String shipinpath,String shichangStr,int X,int Y,int kuan,int gao,String savepath) throws Exception{
    	
    	String dir = savepath.substring(0,savepath.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
		
		
    	List<String> command = new ArrayList<String>();
    	bgpath = bgpath.replaceAll("\\\\","/");
    	bgpath = bgpath.replaceAll("//","/");
    	
    	shipinpath = shipinpath.replaceAll("\\\\","/");
    	shipinpath = shipinpath.replaceAll("//","/");
    	
    	
    	savepath = savepath.replaceAll("\\\\","/");
    	savepath = savepath.replaceAll("//","/");
    	
    	command.add(ffmpeg);
        command.add("-i");
        command.add(bgpath);
        command.add("-i");
        command.add(shipinpath);
        command.add("-y");
        command.add("-filter_complex");
        command.add("[0:v]pad=w=iw:h=ih[bj1];[bj1][1:v]overlay="+X+":"+Y+"");
        command.add("-t");
        if(StringEx.isNotNull(shichangStr)){
        	command.add(shichangStr);
        }
      //调整采样率和分辨率和比特率-------start-----
        command.add("-b:v");
        command.add("5000K");
        if(kuan>0 || gao > 0){
        	command.add("-s");
            command.add(kuan+"x"+gao);
        }
        command.add("-ar");
        command.add("48000");
        command.add("-ac");
        command.add("2");
        command.add(savepath);
        
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }
    
    
    /**
     * @param audioInputPath 原视频的路径
     * @param videoOutPath 音频的路径
     * @param starttime   视频与音频结合之后的视频的路径
     * @param time           视频的长度 ,单位为 s
     * @throws Exception
     */
    // 裁剪视频
    // fmpeg  -i ./plutopr.mp4 -vcodec copy -acodec copy -ss 00:00:10 -to 00:00:15 ./cutout1.mp4 -y
    public void caiJianShiPin(String audioInputPath, String videoOutPath, double starttime,double time)
            throws Exception {
        List<String> command = new ArrayList<String>();
        command.add(ffmpeg);
        command.add("-i");
        command.add(audioInputPath);
        command.add("-vcodec");
        command.add("copy");
        command.add("-acodec");
        command.add("copy");
        command.add("-ss");
        command.add(String.valueOf(starttime));
        command.add("-to");
        command.add(String.valueOf(time));
        command.add(videoOutPath);
        command.add("-y");
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        	System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }
    
 public static void main(String[] args) {
	 
//	 //获取视频时间长度
//	 String mp4path = "f:/aa/bb/b.mp4";
//	 String mp3path = "H:/hunjian/douyin2/caijian/jieshuo/test/1/new.mp4";
	 String mp3path = "H:/hunjian/douyin2/caijian/testtime/1/22/output.mp4";
//	 String mp4hechengpath = "d:/linshi/1550564794487_9821.mp4";
	 String ffmpeg = "E:/ffmpeg-20181007-0a41a8b-win64-static/bin/ffmpeg.exe";
//	 
	 FFMpegUtil ffmpegUtil = new FFMpegUtil();
//	 String shipintime = ffmpegUtil.getVideoTimeStr(mp3path);
	 long shipintime = ffmpegUtil.getVideoTimeHaoMiao(mp3path);
	 System.out.println("视频长度："+shipintime);
//	 
//	 try{
//		 ffmpegUtil.shiPinYinPinHeCheng(mp4path,mp3path,mp4hechengpath,100);
//	 }catch(Exception e){
//		 e.printStackTrace();
//	 }
	 
	 //从视频中抽取mp3音频文件
//	 String shipin = "f:/aa/bb/a.mp4";
//	 String yinpin = "f:/aa/bb/a.aac";
//	 File fileaa = new File(shipin);
//	 System.out.println(fileaa.getPath());
//	 try{
//		 ffmpegUtil.shipinOutYinPin(shipin,yinpin);
//	 }catch(Exception e){
//		 e.printStackTrace();
//	 }
	 
	 //视频截图
//    String videoInputPath = "D:/ziyuan/shipin.mp4";
//    String coverOutputPath = "D:/ziyuan/jietu.jpg";
//    try {
//       convetor("00:00:01", videoInputPath, 1, coverOutputPath);
//    } catch (Exception e) {
//       e.printStackTrace();
//    }
 }
}